using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace CocaCola_Inventory_System.Forms
{
    public partial class Setting : Form
    {
        DataTable data;
        public Setting()
        {
            InitializeComponent();
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            /*
            try
            {

                config c = new config("10.87.212.173",1361,"ITInventorySystem@mena.ko.com","root","kym07");
                //MessageBox.Show(Application.StartupPath + @"\con.config");
                FileStream f = new FileStream("settings.config", FileMode.Create, FileAccess.ReadWrite);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(f, c);
                f.Flush();
                f.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Error saving Settings");
                return;
            }
             */

            backgroundWorker1.RunWorkerAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*int id = 0;
            if (textBox1.Text == "")
                return;
            if (textBox2.Text == "")
                return;
            if (textBox3.Text == "")
                return;
            BinaryFormatter bf = new BinaryFormatter();
            if (comboBox1.SelectedIndex < 0 || comboBox1.SelectedIndex >= comboBox1.Items.Count)
            {
                FileStream read = new FileStream("settings.config", FileMode.Open, FileAccess.Read);

                config c = (config)bf.Deserialize(read);
                id = c.userid;
                read.Flush();
                read.Close();
            }
            else
                id = Convert.ToInt32(data.Rows[comboBox1.SelectedIndex][0]);
            try
            {

                config c = new config(textBox1.Text,id,textBox2.Text,textBox3.Text);
                //MessageBox.Show(Application.StartupPath + @"\con.config");
                FileStream f = new FileStream("settings.config", FileMode.Create, FileAccess.ReadWrite);
                
                bf.Serialize(f, c);
                f.Flush();
                f.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Error saving Settings");
                return;
        }
        MessageBox.Show("Settings Saved Successfully ,\n Please restart the program for the settings to take effect");*/
    }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            retrieve r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            data = r.getusers();
           

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            comboBox1.DataSource = data;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";
        }
}
    }